/**
 * 壳嵌入的原生Js处理逻辑
 * Author: david.deng<david.deng@jcinfotech.com>
 * Date: 2018/6/1
 */
import { isNullOrWhiteSpece } from '../utils/is';

export interface NativeJsInterface {
  getClient: () => string;
  getConfig: (key: string) => string;
  saveConfig: (key: string, value: string) => void;
  getPrinters: () => string;
  print: (setting: string, htmlstring: string) => string;
  print_isCanDuplex: (printer: string) => string;

  heightWeightDevice_Open: () => any;
  heightWeightDevice_Close: () => any;
  heightWeightDevice_StartReceive: () => any;
  heightWeightDevice_StopReceive: () => any;
  heightWeightDevice_GetData: () => any;
  heightWeightDevice_GetStatus: () => any;

  // 客户端打开新窗体
  openWebBrowser: (url: string, title: string) => any;
}

export interface PrintSetting {
  /**
   * 打印机名称
   */
  Printer: string;
  /**
   * A4,A5
   */
  PageSize: string;
  /**
   * 1为纵向 2为横向
   */
  PageDirection: string;
}

export class mockNativeJs implements NativeJsInterface {
  private cecheKeyPre = 'print_config_2018-6-4';
  getClient() {
    return '';
  }

  getConfig(key: string) {
    return window.localStorage.getItem(this.cecheKeyPre + key);
  }

  saveConfig(key: string, value: string) {
    return window.localStorage.setItem(this.cecheKeyPre + key, value);
  }

  getPrinters() {
    return '{"Data":["TestPrinterOne","TestPrinterTwo"],"Status":1}';
  }

  print(setting: string, htmlstring: string) {
    console.log('run mockNativeJs print');
    console.log(setting);
    console.log(htmlstring);
    return '';
  }

  print_isCanDuplex() {
    return '{"Data": true,"Status": 1}';
  }

  heightWeightDevice_Open: () => any;
  heightWeightDevice_Close: () => any;
  heightWeightDevice_StartReceive: () => any;
  heightWeightDevice_StopReceive: () => any;
  heightWeightDevice_GetData: () => any;
  heightWeightDevice_GetStatus: () => any;
  openWebBrowser: (url: string, title: string) => any;
}

export class NativeJsClass {
  native: NativeJsInterface;
  constructor() {
    // @ts-expect-error
    this.native = window.native || new mockNativeJs();
  }

  __getResponseData(jsonStr: string) {
    try {
      const response = JSON.parse(jsonStr);
      if (response && !isNullOrWhiteSpece(response.Data)) {
        return response.Data;
      }
    } catch (e) {
      console.error(`解析 JSON 数据失败, 内容:${jsonStr}`);
      throw e;
    }
    return null;
  }

  getClient() {
    return this.isClient() && this.native.getClient();
  }

  getConfig(key: string) {
    if (this.isClient()) {
      return this.__getResponseData(this.native.getConfig(key));
    }
    return this.native.getConfig(key);
  }

  saveConfig(key: string, value: string) {
    return this.native.saveConfig(key, value);
  }

  /**
   * 是否客户端
   */
  isClient() {
    return this.native.getClient() === 'cef';
  }

  getPrinters(): string[] {
    const data = this.__getResponseData(this.native.getPrinters());
    if (data && Array.isArray(data)) {
      return data;
    }
    return [];
  }

  print(setting: PrintSetting, htmlstring: string) {
    if (this.isClient()) {
      console.log(JSON.stringify(setting));
      const response = this.native.print(JSON.stringify(setting), htmlstring);
      console.log(response);
      return;
    }
    console.log('非客户端, 不能调用客户端打印');
  }

  print_isCanDuplex(printer) {
    const data = this.isClient() && this.__getResponseData(this.native.print_isCanDuplex(printer));
    if (data) {
      return data;
    }
    return false;
  }
}
